package fun.qu_an.minecraft.voicechatplugin;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

public class MultiFuncChatConfig {
	private static final Gson GSON = new GsonBuilder().setVersion(1.0).disableHtmlEscaping().setPrettyPrinting().setLenient().create();
	private static final Path configPath = Qu_anVoiceChatPlugin.DATA_FOLDER.resolve(Qu_anVoiceChatPlugin.MOD_ID + ".json");
	private static MultiFuncChatConfig config;

	private boolean enable = true;
	private boolean teamSpeak = false;

	public static void load() {
		try {
			if (!Files.exists(configPath)) {
				Files.createDirectories(configPath.getParent());
				Files.createFile(configPath);
				Files.write(configPath, GSON.toJson(new MultiFuncChatConfig()).getBytes(StandardCharsets.UTF_8));
			}
			config = GSON.fromJson(new FileReader(configPath.toFile()), MultiFuncChatConfig.class);
			// 写回，补全缺失
			Files.write(configPath, GSON.toJson(config).getBytes(StandardCharsets.UTF_8));
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public static void save() {
		try {
			Files.write(configPath, GSON.toJson(config).getBytes(StandardCharsets.UTF_8));
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public static MultiFuncChatConfig get() {
		return config;
	}

	public boolean isEnable() {
		return enable;
	}

	public void setEnable(boolean enable) {
		this.enable = enable;
	}

	public boolean isTeamSpeak() {
		return teamSpeak;
	}
	public void setTeamSpeak(boolean teamSpeak) {
		this.teamSpeak = teamSpeak;
	}
}
